<template>
  <div id="con">
    <div class="container">


      <h2>{{ isLogin ? '登录' : '注册' }}</h2>
      <dv-decoration-1 style="width:200px;height:50px;"/>
      <form v-if="isLogin" @submit.prevent="login">

        <label for="login-username" style="color: white">用户名：</label>
        <input id="login-username" type="text" v-model="loginUsername" placeholder="请输入用户名" required>
        <br>
        <label for="login-password" style="color: white">密码：</label>
        <input id="login-password" type="password" v-model="loginPassword" placeholder="请输入密码" required
               style="margin-bottom: 20px">
        <br>
        <button type="submit" style="margin-left: 90px">登录</button>
      </form>

      <form v-else @submit.prevent="register">
        <label for="register-username" style="color: white">用户名：</label>
        <input id="register-username" type="text" v-model="registerUsername" placeholder="请输入用户名" required>
        <br>
        <label for="register-password" style="color: white">密码：</label>
        <input id="register-password" type="password" v-model="registerPassword" placeholder="请输入密码" required
               style="margin-bottom: 20px">
        <br>
        <button type="submit" style="margin-left: 90px">注册</button>
      </form>

      <p v-if="isLogin">还没有账号？<a href="#" @click="toggleForm">点击注册</a></p>
      <p v-if="!isLogin">已有账号？<a href="#" @click="toggleForm">点击登录</a></p>


    </div>
  </div>

</template>

<script>
import dvMenu from "_c/dv-menu/index.vue";
import {login, register} from "@/api/login";

export default {
  components: {dvMenu},
  data() {
    return {
      isLogin: true,
      loginUsername: '',
      loginPassword: '',
      registerUsername: '',
      registerPassword: '',
      menus: [
        {title: "分布式系统故障诊断平台", path: "/chat"},
        {title: "模型管理", path: "/model"},
        {title: "数据管理", path: "/data"}
      ]
    };
  },
  methods: {
    toggleForm() {
      this.isLogin = !this.isLogin;
    },
    login() {
      const requestData = {
        username: this.loginUsername,
        password: this.loginPassword
      };

      login(requestData).then(res => {
        console.log('登录结果：');
        console.log(res.data);
        var expirationTime = new Date();
        expirationTime.setDate(expirationTime.getDate() + 1);
        var token = res.data.access_token;
// 将过期时间和 token 存储在 localStorage
        var tokenWithExpiration = {
          token: "JWT " + token,
          expiresAt: expirationTime.getTime() // 将过期时间转换为时间戳
        };
        localStorage.setItem('token', JSON.stringify(tokenWithExpiration));
        this.$message.success('登录成功');
        this.$router.push('/data');
      }).catch(() => {
        this.$message.info('用户不存在,请先注册')
      });
    },
    register() {
      let fromData = new FormData();
      fromData.append("username", this.registerUsername);
      fromData.append("password", this.registerPassword);
      register(fromData).then(res => {
        var result = res.data;
        if (result.code === "200") {
          this.$message.info('注册成功，请登录');
        }
        this.isLogin = true;
      });
    }
  }
};
</script>

<style scoped>
#con {
  background-image: url("pageBg.png");
  height: 1080px;
}

.container {
  height: 700px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;

  color: #333;
}

p {
  color: white;
}

h2 {
  margin-bottom: 20px;
  color: white;
}

form {
  padding: 20px;
  border: 1px solid #ccc;
  border-radius: 5px;
  background-color: rgba(10, 16, 37, 0.9);
  margin-bottom: 50px;
}

input {
  width: 100%;
  padding: 8px;
  margin-bottom: 10px;
  border: 1px solid #ccc;
  border-radius: 4px;
  box-sizing: border-box;
  color: #333;
}

input:required:invalid {
  border-color: red;
}

button {
  padding: 10px 20px;
  background-color: #007bff;
  color: #fff;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  margin: auto;
}

button:disabled {
  background-color: #ccc;
  cursor: not-allowed;
}

a {
  color: #007bff;
  text-decoration: none;
}

a:hover {
  text-decoration: underline;
}

.tech-elements {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}

.circle {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  background-color: #007bff;
  margin: 0 10px;
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.2);
    background-color: #00ccff;
  }
  100% {
    transform: scale(1);
    background-color: #007bff;
  }
}
</style>